<?php
if(!defined('FROM_INDEX'))exit('Invalid request');

require_once ROOT_PATH.'include/logic/user.logic.php';
require_once ROOT_PATH.'include/sql/function/user.sql.php';
require_once ROOT_PATH.'service/base.service.php';
require_once ROOT_PATH.'service/feed.service.php';
require_once ROOT_PATH.'include/logic/image.logic.php';

class UserService extends BaseService
{
	private $sqlFunc;
	private $feedService;
	
	function __construct()
	{
		parent::__construct();
		
		$this->sqlFunc = new UserSqlFunc();
		$this->feedService = new FeedService();
	}
	
	/**
	 *
	 * 用户注册
	 * @param array $request
	 */
	function register($request)
	{
		$image = isset($request['image']) ? $request['image'] : '';
		$request['image'] = '';
		
		if($image)
		{
/*
			$tmp_img = save_image_file($image);
			if(isset($tmp_img['name']) && $tmp_img['name'])
			{
				
				$_FILES['image'] = $tmp_img;
				$image_path = move_image_to_path('image', $image_dir);
				if($image_path)
				{
					$request['image'] = $image_path;
				}
			}
*/
			
			$rd = rand(1, 99);
			//$pkg_name = 'user-'.$request['uname'].'-'.$request['uid'];
			$img_name = '/user/'.$rd.'/'.$request['uid'].'-'.time().'.jpg';
			$url = save_image($img_name, $image);
			if ($url != '') $request['image'] = $url;
		}
		
		$request['time'] = time();
		$request['password'] = generate_password($request['password'], $request);
		$request['salt'] = substr(md5($request['email']), 0, 12);
		
		return $this->sqlFunc->add($request);
	}

	/**
	 *
	 * 用户登录
	 * @param array $request
	 */
	function login($request)
	{
		//echo "6--- ".microtime()." ---\r\n";
		
		$uid = ($request['source_id'] != '') ? $request['source_id'] : $request['email'];
		$user = $this->sqlFunc->get($uid);
		
		//echo "7--- ".microtime()." ---\r\n";
		
		if (check_array($user) && isset($user['id']))
		{
			$user['time'] = $user['register_time'];
			if (verify_password($request['password'], $user)) 
			{
				//echo "8--- ".microtime()." ---\r\n";
				
				if ($request['source_id'] != '')
				{
					if ($request['name'] != '' && $request['name'] != $user['name']) $user['name'] = $request['name'];
					if ($request['image'] != '' && $request['image'] != $user['image']) $user['image'] = $request['image'];
					if ($request['desc'] != '' && $request['desc'] != $user['desc']) $user['desc'] = $request['desc'];
					$this->sqlFunc->update($user);
					
					//echo "9--- ".microtime()." ---\r\n";
				}
				
				unset($user['time']);
				$user['image'] = get_resource_url($user['image']);
				return $user;
			}
		}
		else if ($request['source_id'] != '')
		{
			$image = isset($request['image']) ? $request['image'] : '';
			
			if($image)
			{
				$rd = rand(1, 99);
				//$pkg_name = 'user-'.$request['uname'].'-'.$request['uid'];
				$img_name = '/user/'.$rd.'/'.$request['uid'].'-'.time().'.jpg';
				$url = save_image($img_name, $image);
				if ($url != '') $request['image'] = $url;
			}
			
			if (isset($request['name'])) {
				$user = $this->get($request['name']);
				if (check_array($user)) {
					$request['name'] .= '1';
				}
			}
			
			$request['time'] = time();
			$request['password'] = generate_password($request['password'], $request);
			$request['salt'] = substr(md5($request['email']), 0, 12);
			
			$result =  $this->sqlFunc->add($request);
			
			if ($result) return $this->get($result);
		}

		return false;
	}

	/**
	 *
	 * 获取用户信息
	 * @param String $uid
	 */
	function get($id, $uid='')
	{
		$user = $this->sqlFunc->get($id);
		if (check_array($user) && isset($user['id']))
		{
			$is_follow = $this->sqlFunc->check_follow(array('uid'=>$uid, 'id'=>$id));
			$user['is_follow'] = $is_follow ? true : false;
			$user['image'] = get_resource_url($user['image']);
			$user['register_time'] = format_time($user['register_time']);
		}
		
		return $user;
	}

	/**
	 *
	 * 用户搜索
	 * @param array $request
	 */
	function search($request)
	{
		$result = $this->sqlFunc->search($request);
		
		if (check_array($result))
		{
			foreach ($result['list'] as &$arr)
			{
				if (isset($arr['register_time']))
				{
					$arr['image'] = get_resource_url($arr['image']);
					$user['register_time'] = format_time($user['register_time']);
				}
			}
		}
		
		return check_array($result)? $result : array();
	}

	/**
	 *
	 * 更新用户信息
	 * @param array $request
	 */
	function update($request)
	{
		$info = $this->get($request['id']);
		
		$result = false;
		if (check_array($info))
		{
			foreach ($request as $k=>$v) {
				if ($v != '' && array_key_exists($k, $info) && $k != 'password') $info[$k] = $v;
			}
			if ($request['email'] != '' && $request['password'] != '') $info['password'] = generate_password($request['password'], $request);
			
			if ($request['time'] != '') {
				$info['time'] = $request['time'];
				$this->sqlFunc->update_last_login($info);
			}
			
			$image = isset($request['image']) ? $request['image'] : '';
			
			if($image)
			{
				$rd = rand(1, 99);
				//$pkg_name = 'user-'.$request['uname'].'-'.$request['uid'];
				$img_name = '/user/'.$rd.'/'.$request['uid'].'-'.time().'.jpg';
				$url = save_image($img_name, $image);
				if ($url != '') {
					if ($info['image'] != '') delete_from_bcs($info['image']);
					$info['image'] = $url;
				}
			}
			
			$result = $this->sqlFunc->update($info);
		}
		
		return $result;
	}
	
	function update_story_count($uid)
	{
		return $this->sqlFunc->update_story_count($uid);
	}
	
	function update_follow_count($uid)
	{
		return $this->sqlFunc->update_follow_count($uid);
	}
	
	function update_tag_count($uid)
	{
		return $this->sqlFunc->update_tag_count($uid);
	}

	/**
	 *
	 * 获取用户关注列表
	 * @param array $request
	 */
	function get_follow_list($request)
	{
		return $this->sqlFunc->get_follow_list($request);
	}
	
	function add_follow($request)
	{
		$result = false;
		
		$user = $this->get($request['id']);
		if (check_array($user)) {
			$result = $this->sqlFunc->add_follow($request);
			if ($result) {
				$this->sqlFunc->update_follow_count($request);
				$this->feedService->add(array('uid'=>$request['uid'],'fuid'=>$request['id'],'type'=>'f', 'time'=>time()));
			}
		}
		
		return $result;
	}
	
	function remove_follow($request)
	{
		$result = false;
		
		$user = $this->get($request['id']);
		if (check_array($user)) {
			$result = $this->sqlFunc->remove_follow($request);
			if ($result) {
				$this->sqlFunc->update_follow_count($request);
				//$this->feedService->add(array('uid'=>$request['uid'],'fuid'=>$request['id'],'type'=>'f', 'time'=>time()));
			}
		}
		
		return $result;
	}
	
	function check_follow($request)
	{
		return $this->sqlFunc->check_follow($request);
	}

	/**
	 *
	 * 获取用户粉丝列表
	 * @param array $request
	 */
	function get_fans_list($request)
	{
		return $this->sqlFunc->get_fans_list($request);
	}
	
	function get_user_recommend($request)
	{
		return $this->sqlFunc->get_user_recommend($request);
	}
	
	function add_feedback($request)
	{
		return $this->sqlFunc->add_feedback($request);
	}
}